package org.anderground.editor.console;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;

import org.anderground.core.Logger;

public class ArgMatcher {
	private LinkedList<Arg> args = new LinkedList<Arg>();
	private List<String> files = new ArrayList<String>();
	private List<String> folders = new ArrayList<String>();
	public ArgMatcher(String[] args) {
		for(int i=0;i<args.length;i++) {
			
			if(args[i].charAt(0)=='-') {
				String name = args[i].substring(1);
				this.args.add(new Arg(name));
			}
			else {
				File file = new File(args[i]);
				if(file.isFile()) {
					files.add(args[i]);
				} else if(file.isDirectory()) {
					folders.add(args[i]);
				} else {
					try {
						this.args.getLast().addSubarg(args[i]);
					} catch(NoSuchElementException e) {
						Logger.w("file not found:" + args[i]);
					}
				}
			}
		}
	}
	
	public boolean haveArg(String name) {
		return getArg(name)!=null;
	}
	public Arg getArg(String name) {
		for(Arg arg:args) {
			if(arg.name.equals(name)) {
				return arg;
			}
		}
		return null;
	}
	public Arg[] getArgs() {
		return args.toArray(new Arg[0]);
	}
	public String[] getFiles() {
		return files.toArray(new String[0]);
	}
	public String[] getFolders() {
		return folders.toArray(new String[0]);
	}
	
	
	
}




